package cn.candy.sys.controller;

import cn.candy.basic.util.AjaxResult;
import cn.candy.basic.util.PageList;
import cn.candy.sys.domain.DictionaryType;
import cn.candy.sys.query.DictionaryTypeQuery;
import cn.candy.sys.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 数据字典类型接口类
 */
@RestController
@RequestMapping("/dictionaryType")
@Api(tags = "数据字典类型接口",description = "数据字典类型接口详细描述")
public class DictionaryTypeController {

    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    /**
     * 查询所有对象接口：
     * @return - 返回查询的结果
     */
    @GetMapping
    @ApiOperation(value = "查询所有接口",notes = "")
    public List<DictionaryType> findAll(){
        return dictionaryTypeService.findAll();
    }

    /**
     * 查询单个对象接口
     * @param id - 传递查询对象的id
     * @return
     */
    @ApiOperation(value = "查询单个接口",notes = "需要传递id")
    @GetMapping("/{id}")   //用{}区分是请求参数，没有用{}是请求地址
    public DictionaryType findOne(@PathVariable("id")Long id){
        return dictionaryTypeService.findOne(id);
    }

    /**
     * 分页查询接口
     * @param dictionaryTypeQuery - 分页参数 + 高级查询参数
     * @return 查询结果 - totals data
     */
    @ApiOperation(value = "分页查询接口",notes = "需要传递分页数据")
    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery dictionaryTypeQuery){
        return dictionaryTypeService.queryPage(dictionaryTypeQuery);
    }

    /**
     * 删除接口
     * @param id - 传递需要删除的id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口",notes = "需要传递id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            dictionaryTypeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败，" + e.getMessage());
        }
    }
    /**
     * 添加或修改接口
     * @param dictionaryType
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口",notes = "需要传递对象")
    public AjaxResult saveOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if(dictionaryType.getId()==null){//添加操作
                dictionaryTypeService.add(dictionaryType);
            }else{
                dictionaryTypeService.update(dictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败，" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids - 需要传递删除对象的id
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口",notes = "需要传递删除对象的id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            dictionaryTypeService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("批量删除失败，" + e.getMessage());
        }
    }
}
